<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/20
 * Time: 12:17 AM
 */

namespace App\HttpController;

use App\Utility\Pool\RedisPool;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Session\AbstractSessionController;
use EasySwoole\Session\Test\RedisHandler;
use EasySwoole\Session\FileSessionHandler;
use think\Template;

abstract class ViewController extends AbstractSessionController
{

    /**
     * 视图实例对象
     * @var view
     * @access protected
     */
    protected $view = null;
    protected $module = ''; //模型
    protected $action_method = '';  //操作方法
    protected $controller = '';  //操作控制器

    protected function sessionHandler(): \SessionHandlerInterface
    {
        /*
         * 此处应该由连接池拿链接，否则实际生产会导致不断创建链接
         */
//        $redis = RedisPool::defer();
//        return new RedisHandler($redis);
        return new FileSessionHandler();
    }

    public function __construct($path = '')
    {

        if (method_exists($this, '_initialize'))
            $this->_initialize();
        $this->controller = $controller = basename(str_replace('\\', '/', get_called_class()));
        $conf = \EasySwoole\EasySwoole\Config::getInstance()->getConf('TEMPLATE');
        $this->view = new Template($conf);
        parent::__construct();

    }

    /**
     * 设置模型
     * @param $module
     */
    public function setModule($module)
    {
        $this->module = $module;
    }

    /**
     * 设置数径
     * @param $viewpath
     */
    public function setActionMethod($action)
    {
        $this->action_method = $action;
    }

    /**
     * 设置controller
     */
    public function setController($controller)
    {
        $this->controller = $controller;
    }

    /**
     * 获取模型
     * @return string
     */
    public function getModule()
    {
        return $this->module;
    }

    /**
     * 获取控制器
     * @return string
     */
    public function getActionController()
    {
        return $this->controller;
    }

    /**
     * 操作方法
     * @return string|null
     */
    public function getActionMethod()
    {
        return $this->action_method;
    }


    /**
     *  获取输出页面内容
     * 调用内置的模板引擎fetch方法，
     * @access protected
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @param string $content 模板输出内容
     * @param string $prefix 模板缓存前缀*
     * @return string
     */

    public function fetch(string $template = '', array $vars = []): void
    {
        if (!empty($this->module)) {
            $templatePath[] = $this->module;
            $vars['operate']['module'] = $this->module;
        }
        if (!empty($this->controller)) {
            $templatePath[] = $this->controller;
            $vars['operate']['controller'] = $this->controller;
        }
        $this->action_method = $this->getActionName();
        $templatePath[] = !empty($template) ? $template : $this->action_method;
        $vars['operate']['action'] = !empty($template) ? $template : $this->action_method;
        $templateFile = implode("/", $templatePath);
        $this->view->fetch($templateFile, $vars);
        $content = ob_get_clean();
        $this->response()->write($content);
    }

//    /**
//     * 模板变量赋值
//     * @access protected
//     * @param mixed $name 要显示的模板变量
//     * @param mixed $value 变量的值
//     * @return Action
//     */
//    protected function assignData(array $vars = [])
//    {
//        return $this->view->assign($vars);
//    }

    /**
     * 模板变量赋值
     * @access public
     * @param mixed $name
     * @param mixed $value
     * @return void
     */
    public function assign($vars)
    {
        return $this->view->assign($vars);
    }

    /**
     * 取得模板显示变量的值
     * @access protected
     * @param string $name 模板显示变量
     * @return mixed
     */

    public function getView(): Template
    {
        return $this->view;
    }
}